<template>
	<!-- 
	swiper中的transfrom会使fixed失效,此时用height固定高度; 
	swiper中无法触发mescroll-mixins.js的onPageScroll和onReachBottom方法,只能用mescroll-uni,不能用mescroll-body
	-->
	<!-- ref动态生成: 字节跳动小程序编辑器不支持一个页面存在相同的ref (如不考虑字节跳动小程序可固定值为 ref="mescrollRef") -->
	<!-- top的高度等于悬浮菜单tabs的高度 -->
	<mescroll-uni :ref="'mescrollRef' + i" @init="mescrollInit" :height="height" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @emptyclick="emptyClick">
		<!-- 数据列表 -->
		<u-gap height="20" bg-color="#ffffff"></u-gap>
		<view class="uni-block-list">
			<view class="item u-flex" v-for="(item, i) in goods" :key="i">
				<view class="img_box" @click="togoods(item.id)"><vimage :src="item.master"></vimage></view>
				<view class="content u-row-between u-flex u-flex-col u-col-top u-flex u-flex-col u-col-top">
					<view class="view">
						<text class="u-line-2">{{ item.name }}</text>
						<text class="u-line-1">{{ item.subtitle }}</text>
					</view>

					<view class="u-flex u-row-between b_t">
						<view class="price">
							<text>{{ item.sell_price }}</text>
							<text>积分</text>
						</view>
						<view class="btns" @click="gotrolley(item)"><text>兑换</text></view>
					</view>
				</view>
			</view>
		</view>
		<uni-login ref="Login" @callback="logincallback"></uni-login>
	</mescroll-uni>
</template>

<script>
import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
import MescrollMoreItemMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js';
// import {apiGoods} from "@/api/mock.js"
import util from '@/common/util.js';
import http from '@/common/http.js';
export default {
	mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
	data() {
		return {
			downOption: {
				auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
			},
			upOption: {
				auto: false, // 不自动加载
				page: {
					num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					size: 10 // 每页数据的数量
				},
				noMoreSize: 10, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
				empty: {
					tip: '暂无数据', // 提示
					btnText: '',
					icon: util.getimgurl('static/images/mescroll-empty.png', '1')
				}
			},
			goods: [], //列表数据
			hasShow: 'none',
			page: 1,
			size: 10,
			showCommon: false
		};
	},
	props: {
		i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
		index: {
			// 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			type: Number,
			default() {
				return 0;
			}
		},
		tabs: {
			// 为了请求数据,演示用,可根据自己的项目判断是否要传
			type: Array,
			default() {
				return [];
			}
		},
		height: [Number, String] // mescroll的高度
	},
	methods: {
		gotrolley(item) {
			let data = {
				show: true,
				id: item.id,
				type:2//积分商品
			};
			this.$store.commit('settrolley', data);
		},
		/*下拉刷新的回调 */
		downCallback() {
			// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
			// loadSwiper();
			// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			this.mescroll.resetUpScroll();
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback(page) {
			//联网加载数据
			let keyword = this.tabs[this.i].name;
			this.getOrderList(page);

			// apiGoods(page.num, page.size, keyword).then(res=>{
			// 	//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
			// 	this.mescroll.endSuccess(res.list.length);
			// 	//设置列表数据
			// 	if(page.num == 1) this.goods = []; //如果是第一页需手动制空列表
			// 	this.goods=this.goods.concat(res.list); //追加新数据
			// }).catch(()=>{
			// 	//联网失败, 结束加载
			// 	this.mescroll.endErr();
			// })
		},
		//点击空布局按钮的回调
		emptyClick() {
			uni.showToast({
				title: '点击了按钮,具体逻辑自行实现'
			});
		},
		getOrderList(page) {
			let data = this.tabs[this.i];
			data.status = 'loading';
			const _this = this;

			const param = {
				page: page.num,
				size: page.size,
				classify_id: data.id
			};

			http('/integral/shop/goods/list', param, 'POST').then(
				res => {
					const json = res.data.msg;
					this.mescroll.endSuccess(json.data.length);
					let list = json.data;
					// list.forEach(order => {
					// 	order.num = 0;
					// 	order.statustext = util.getstatustext(order.status, order.order_type);
					// 	order.orderCommodity.forEach(item => {
					// 		order.num += item.count;
					// 		item.fmoney = item.money / item.count;
					// 		item.fmoney = item.fmoney.toFixed(2);
					// 	});
					// 	let allprice = Number(order.freight) + Number(order.money);
					// 	order.allprice = allprice.toFixed(2);
					// });
					//设置列表数据
					if (page.num == 1) this.goods = []; //如果是第一页需手动制空列表
					this.goods = this.goods.concat(list); //追加新数据
					//
					if (list.length < page.size) {
						this.showCommon = true;
					}
				},
				e => {
					uni.showModal({
						title: '提示',
						content: e,
						showCancel: false
					});
					this.mescroll.endErr();
				}
			);
		},
		logincallback() {
			if (e.islogin) {
				this.mescroll.resetUpScroll();
			}
		},
		topay(item) {
			let list = [];
			list.push(item.order_no);
			list = list.join(',');
			let money = item.allprice;

			uni.navigateTo({
				url: 'pay?total=' + money + '&list=' + list
			});
		},
		goDetail(obj) {
			this.Jumpurl({
				link: '/pointsmallpage/order/order?gid=' + obj.id,
				type: 1,
				login_type: 2
			});
		},
		togoods(id) {
			this.Jumpurl({
				type: 1,
				link:'../goods/detail?id='+id
			});
		},
		cancelOrder(id) {
			const _this = this;
			uni.showModal({
				title: '是否确认取消？',
				success(res) {
					if (res.confirm) {
						const param = {
							id: id
						};
						http('/order/orderDel/' + id, {}, 'DELETE').then(
							res => {
								if (res.statusCode == 201) {
									_this.mescroll.resetUpScroll();
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									});
								}
							},
							e => {
								uni.showModal({
									title: '提示',
									content: e,
									showCancel: false
								});
							}
						);
					}
				}
			});
		},
		delOrder(id) {
			const _this = this;
			const param = {
				id: id
			};
			http('/order/delete', param, 'POST').then(
				res => {
					const json = res.data;
					if (json.code == 0) {
						_this.mescroll.resetUpScroll();
					} else {
						uni.showModal({
							title: '提示',
							content: json.msg,
							showCancel: false
						});
					}
				},
				e => {
					uni.showModal({
						title: '提示',
						content: e,
						showCancel: false
					});
				}
			);
		},
		refundOrder(obj) {
			uni.navigateTo({
				url: '/pages/mine/order/detail?id=' + obj.id + '&refund=' + obj.refundState
			});
		},
		confirmOrder(obj) {
			const _this = this;
			uni.showModal({
				title: '是否确认收货？',
				success(res) {
					if (res.confirm) {
						let orderCommodity = obj.orderCommodity;
						let cids = [];
						orderCommodity.forEach(a => {
							if (a.status == 3) {
								cids.push(a.ocid);
							}
						});
						let param = {
							id: obj.id,
							cids: JSON.stringify(cids)
						};
						http('/order/confirm', param, 'PUT').then(res => {
							if (res.statusCode == 200) {
								_this.mescroll.resetUpScroll();
							} else {
							}
						});
					}
				}
			});
		},
		accMul(arg1, arg2) {
			var m = 0,
				s1 = arg1.toString(),
				s2 = arg2.toString();
			try {
				m += s1.split('.')[1].length;
			} catch (e) {}
			try {
				m += s2.split('.')[1].length;
			} catch (e) {}
			return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
		}
	}
};
</script>
<style lang="scss">
.uni-block-list {
	// padding-top: 100rpx !important;
	background: #ffffff;
	.item {
		padding: 30rpx 22rpx;
		.img_box {
			width: 160rpx;
			height: 160rpx;
			/deep/.img_box {
				width: 160rpx;
				height: 160rpx;
			}
			// background-color: #000000;
		}
		.content {
			height: 160rpx;
			flex: 1;
			margin-left: 16rpx;
			.view {
				> text:first-child {
					width: 500rpx;
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 400;
					line-height: 42rpx;
					color: #333333;
				}
				> text:nth-child(2) {
					height: 32rpx;
					font-size: 22rpx;
					font-family: PingFang SC;
					font-weight: 400;
					line-height: 32rpx;
					color: #c2c2c2;
				}
			}

			width: 100%;
			.b_t {
				width: 100%;
				.price {
					font-size: 34rpx;
					font-family: PingFang SC;
					font-weight: bold;
					line-height: 48rpx;
					color: #fb4743;
					text:last-child {
						margin-left: 8rpx;
						font-size: 22rpx;
						font-family: PingFang SC;
						font-weight: 400;
						line-height: 32rpx;
						color: #fb4743;
					}
				}
				.btns {
					width: 142rpx;
					height: 48rpx;
					background: linear-gradient(295deg, #fb4743 0%, #fe5c34 100%);
					opacity: 1;
					border-radius: 24rpx;
					text-align: center;
					line-height: 48rpx;
					color: #ffffff;
					font-size: 26rpx;
				}
			}
		}
	}
}
</style>
